﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLControlLibrary.Resources.Controls.Calendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.Buttons;
using SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar;

namespace SLControlLibrary.Resources.Controls.Calendar.DatePicker
{
    public class ucDatePicker : Control
    {
        #region Dependency Variables
        private static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(ucDatePicker), new PropertyMetadata(DateTime.Today.ToShortDateString()));
        public static readonly DependencyProperty SelectedDateProperty = DependencyProperty.Register("SelectedDate", typeof(DateTime), typeof(ucDatePicker), new PropertyMetadata(DateTime.Today, new PropertyChangedCallback(SelectedDate_Changed)));
        #endregion

        #region Dependency Properties
        public string Value
        {
            get { return this.GetValue(ValueProperty).ToString(); }
            set { this.SetValue(ValueProperty, value); }
        }
        public DateTime SelectedDate
        {
            get { return (DateTime)this.GetValue(SelectedDateProperty); }
            set { this.SetValue(SelectedDateProperty, value); }
        }
        #endregion

        #region Dependency Events
        #region SelectedDate_Changed
        private static void SelectedDate_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDatePicker).OnSelectedDate_Changed(e);
        }
        #endregion
        #endregion

        #region Variables
        private ucCalendarButton btnCalendar = null;
        private ucCalendar Calendar = null;
        private Border brdCalendar = null;
        private bool IsDateChangeFromLoad = false;
        #endregion

        #region Event Variables
        public event Events.DateSelectedHandler DateSelectedEvent;
        #endregion

        #region Constructor
        public ucDatePicker()
        {
            DefaultStyleKey = typeof(ucDatePicker);
        }
        #endregion

        #region Methods
        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            btnCalendar = (ucCalendarButton)base.GetTemplateChild("btnCalendar");
            Calendar = (ucCalendar)base.GetTemplateChild("Calendar");
            brdCalendar = (Border)base.GetTemplateChild("brdCalendar");

            if (btnCalendar == null) return;
            if (Calendar == null) return;
            if (brdCalendar == null) return;

            btnCalendar.Click += new RoutedEventHandler(btnCalendar_Click);
            Calendar.SelectedDatesChanged += new EventHandler<SelectionChangedEventArgs>(Calendar_SelectedDatesChanged);
            Calendar._CurrentMonth = DateTime.Now.Month;
            Calendar._CurrentYear = DateTime.Now.Year;

            Calendar._CurrentMonth = SelectedDate.Month;
            Calendar._CurrentYear = SelectedDate.Year;
            Calendar.SelectedDate = SelectedDate;
        }
        #endregion

        #region OnSelectedDate_Changed
        protected virtual void OnSelectedDate_Changed(DependencyPropertyChangedEventArgs e)
        {
            SelectedDate = (DateTime)e.NewValue;
            Value = SelectedDate.ToShortDateString();

            if (Calendar != null)
            {
                Calendar._CurrentMonth = SelectedDate.Month;
                Calendar._CurrentYear = SelectedDate.Year;
                Calendar.SelectedDate = SelectedDate;
            }
        }
        #endregion
        #endregion

        #region Events
        #region btnCalendar_Click
        void btnCalendar_Click(object sender, RoutedEventArgs e)
        {
            IsDateChangeFromLoad = true;

            if (brdCalendar.Visibility == Visibility.Visible)
            {
                brdCalendar.Visibility = Visibility.Collapsed;
                btnCalendar.ButtonType = Enumerations.CalendarButtonType.Down;
            }
            else if (brdCalendar.Visibility == Visibility.Collapsed)
            {
                brdCalendar.Visibility = Visibility.Visible;
                btnCalendar.ButtonType = Enumerations.CalendarButtonType.Up;
            }
        }
        #endregion

        #region Calendar_SelectedDatesChanged
        void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (DateTime _Day in (sender as ucCalendar).SelectedDates)
                Value = _Day.ToShortDateString();

            if (!IsDateChangeFromLoad)
            {
                brdCalendar.Visibility = Visibility.Collapsed;
                btnCalendar.ButtonType = Enumerations.CalendarButtonType.Down;

                if (DateSelectedEvent != null)
                    DateSelectedEvent(DateTime.Parse(Value), null);
            }
        }
        #endregion
        #endregion
    }
}
